<template>
  <div
    class="product-card"
    @click="handleClick"
  >
    <div class="product-image">
      <img
        :src="product.img"
        :alt="product.name"
      />
    </div>
    <div class="product-info">
      <div class="product-name">{{ product.name }}</div>
      <div class="product-price">¥{{ product.price }}</div>
    </div>
    <div
      v-if="showAddToCart"
      class="add-to-cart-btn"
      @click.stop="handleAddToCart"
    >
      <SvgIcon
        name="shop.cart"
        size="16"
      />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import SvgIcon from '../SvgIcon.vue'

const props = defineProps({
  product: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  showAddToCart: {
    type: Boolean,
    default: false,
  },
  lazyLoad: {
    type: Boolean,
    default: false,
  },
})

const emit = defineEmits(['click', 'add-to-cart'])

// 格式化价格显示
const formattedPrice = computed(() => {
  if (!props.product.price) return '¥0'
  return typeof props.product.price === 'number'
    ? `¥${props.product.price.toFixed(2)}`
    : `¥${props.product.price}`
})

// 处理卡片点击
const handleClick = () => {
  emit('click', props.product)
}

// 处理加入购物车
const handleAddToCart = () => {
  emit('add-to-cart', props.product)
}
</script>

<style scoped lang="less">
@import (reference) '../../styles/global.less';

.product-card {
  background: @white;
  border-radius: @border-radius-lg;
  overflow: hidden;
  box-shadow: @box-shadow-lg;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  cursor: pointer;
  position: relative;

  &:hover {
    transform: translateY(-5px);
    box-shadow: @box-shadow-lg;

    .product-image img {
      transform: scale(1.05);
    }
  }

  .product-image {
    width: 100%;
    height: 200px;
    overflow: hidden;
    position: relative;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.3s ease;
    }
  }

  .product-info {
    padding: 10px;

    .product-name {
      font-size: @font-size-sm;
      color: @text-color;
      height: 40px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      margin-bottom: 8px;
    }

    .product-price {
      font-size: 16px;
      font-weight: bold;
      color: @danger-color;
    }
  }

  .add-to-cart-btn {
    position: absolute;
    right: 10px;
    bottom: 10px;
    background: @success-color;
    color: @white;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease;

    &:hover {
      background: #06b355;
      transform: scale(1.1);
    }
  }

  // 响应式调整
  @media (max-width: @screen-md) {
    .product-image {
      height: 150px;
    }
  }
}
</style>
